package com.sangeng.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sangeng.domain.entity.Article;
import com.sangeng.service.ArticleService;
import com.sangeng.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TestJob {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ArticleService articleService;
    /*
    定时任务
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void testJob(){
        //要执行的代码
        System.out.println("定时任务执行");
//        updataViewCount();
    }

    /**
     * 更新文章浏览量
     */
    private void updataViewCount() {
        List<Article> list = articleService.list();
        list.forEach(item->{
            Long articleViewCount=redisCache.getCacheObject("articleViewCount:"+item.getId());
            Article article=new Article();
            article.setViewCount(articleViewCount);
            article.setId(item.getId());
            articleService.updateById(article);
        });
    }
}
